/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fjnu.dao.mysql;

import fjnu.dao.AbstractObject;
import fjnu.dao.Lamp;
import fjnu.dao.factory.AbstractLampMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author neco
 */
public class MysqlLampMapper extends AbstractLampMapper {

    private static final String readSQL = "SELECT * FROM fjnu.lamp WHERE ID=";
    private static final String readTypeSQL = "SELECT * FROM fjnu.lampdet WHERE Type='";
    private static final String FixSQL = "UPDATE `fjnu`.`lamp` SET `Broken`=0 WHERE `ID`=";
    private static final String BrokenSQL = "UPDATE `fjnu`.`lamp` SET `Broken`=`Broken`+1 WHERE `ID`=";
    private static final String readBorkenSQL = "SELECT ID FROM fjnu.lamp WHERE Broken>0;";

    @Override
    public AbstractObject getObjectByID(int ID) {
        Lamp mLamp = new Lamp();
        MysqlStatement mysqlState = new MysqlStatement();
        try {
            ResultSet rs = mysqlState.excuteSQL(readSQL + ID + ";");
            mLamp = BaseMapper(rs, mLamp);
            ResultSet rs2 = mysqlState.excuteSQL(readTypeSQL + mLamp.getType() + "';");
            mLamp = ExMapper(rs2, mLamp);

        } catch (SQLException ex) {
            Logger.getLogger(MysqlLampMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.addObjectToCache(mLamp);
        return mLamp;
    }

    @Override
    public void fix(AbstractObject o, Object arg) {
        MysqlStatement mysqlState = new MysqlStatement();
        mysqlState.excuteSQLNotResult(FixSQL + o.getID() + ";");
    }

    @Override
    public void broken(AbstractObject o, Object arg) {
        MysqlStatement mysqlState = new MysqlStatement();
        mysqlState.excuteSQLNotResult(BrokenSQL + o.getID() + ";");
    }

    public Lamp BaseMapper(ResultSet rs, Lamp lamp) throws SQLException {
        if (rs.next()) {
            lamp.setID(rs.getInt("ID"));
            lamp.setType(rs.getString("Type"));
            Date date = (java.util.Date) rs.getDate("BuyTime");
            lamp.set_Broken(rs.getInt("Broken"));
            lamp.setBuyTime(date);
        }
        return lamp;
    }

    public Lamp ExMapper(ResultSet rs, Lamp lamp) throws SQLException {
        if (rs.next()) {
            lamp.setBrand(rs.getString("Brand"));
            lamp.setModel(rs.getString("Model"));
            lamp.setLampType(rs.getString("LampType"));
            lamp.setColor(rs.getString("Color"));
            lamp.setStyle(rs.getString("Style"));
            lamp.setPower(rs.getString("Power"));
            lamp.setVoltage(rs.getString("Voltage"));
            lamp.setPrice(rs.getFloat("Price"));
            lamp.setLampCount(rs.getInt("LampCount"));
            lamp.setHeight(rs.getString("Height"));
            lamp.setSupplier(rs.getString("Supplier"));
        }
        return lamp;
    }

    @Override
    public  ArrayList<Integer> getBrokenList() {
         ArrayList<Integer> result = new ArrayList<Integer>();
        try {
            MysqlStatement mysqlState = new MysqlStatement();
            ResultSet rs = mysqlState.excuteSQL(readBorkenSQL);      
            while (rs.next()) {
                result.add(rs.getInt("ID"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MysqlBulletinBoardMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public AbstractObject getObjectByIDNotCache(int ID) {
         Lamp mLamp = new Lamp();
        MysqlStatement mysqlState = new MysqlStatement();
        try {
            ResultSet rs = mysqlState.excuteSQL(readSQL + ID + ";");
            mLamp = BaseMapper(rs, mLamp);
            ResultSet rs2 = mysqlState.excuteSQL(readTypeSQL + mLamp.getType() + "';");
            mLamp = ExMapper(rs2, mLamp);

        } catch (SQLException ex) {
            Logger.getLogger(MysqlLampMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
      //  this.addObjectToCache(mLamp);
        return mLamp;
    }
}
